package com.demo03.test01;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadScopeShareData {
    //private static int data = 0;
    private static Map<Thread, Integer> shareData = new HashMap<>();

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt(100);
                    System.out.println(Thread.currentThread().getName() + " : " + data);
                    shareData.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }

    static class A {
        public void get() {
            int data = shareData.get(Thread.currentThread());
            System.out.println(" A: " + Thread.currentThread().getName() + " : " + data);
        }
    }

    static class B {
        public void get() {
            int data = shareData.get(Thread.currentThread());
            System.out.println(" B: " + Thread.currentThread().getName() + " : " + data);
        }
    }
}
